package com.ymatou.iapi.liveseller.testcase;

import java.util.List;
import java.util.Random;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.QueryLiveProductIdListBean;
import com.ymatou.iapi.liveseller.service.QueryLiveProductIdListCall;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
/**
*查询所有已加入直播的商品Id
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryLiveProductIdList{
	private static QueryLiveProductIdListBean queryliveproductidlistBean;
	private static QueryLiveProductIdListCall queryliveproductidlistCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询所有已加入直播的商品Id");
	}

	@Before
	public void caseUp() {
		queryliveproductidlistBean=new QueryLiveProductIdListBean();
		queryliveproductidlistCall=new QueryLiveProductIdListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryliveproductidlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryLiveProductIdList_001() {
		Logger.start(true,"查询所有已加入直播的商品Id成功");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			queryliveproductidlistBean.setLiveId(createlive.getId());
			queryliveproductidlistCall.setData(queryliveproductidlistBean);
			queryliveproductidlistCall.callService();			
			Logger.verifyEquals("200", queryliveproductidlistCall.getString("Code"), "验证Code");
			Logger.verifyEquals(1,queryliveproductidlistCall.getProductList().size(),"验证商品数量");
			Logger.verifyEquals(pid,queryliveproductidlistCall.getProductList().get(0).getAsString(),"验证pid");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_QueryLiveProductIdList_002() {
		Logger.start(true,"不输入liveid失败");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
//			queryliveproductidlistBean.setLiveId(createlive.getId());
			queryliveproductidlistCall.setData(queryliveproductidlistBean);
			queryliveproductidlistCall.callService();			
			Logger.verifyEquals("400", queryliveproductidlistCall.getString("Code"), "验证Code");
			Logger.verifyEquals("直播Id不能为空", queryliveproductidlistCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryLiveProductIdList_003() {
		Logger.start(true,"查询列表中包含暂停销售商品");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SuspendLiveProduct(createlive.getId(), pid);
			queryliveproductidlistBean.setLiveId(createlive.getId());
			queryliveproductidlistCall.setData(queryliveproductidlistBean);
			queryliveproductidlistCall.callService();			
			Logger.verifyEquals("200", queryliveproductidlistCall.getString("Code"), "验证Code");
			Logger.verifyEquals(1,queryliveproductidlistCall.getProductList().size(),"验证商品数量");
			Logger.verifyEquals(pid,queryliveproductidlistCall.getProductList().get(0).getAsString(),"验证pid");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryLiveProductIdList_004() {
		Logger.start(true,"查询列表中不包含删除商品action-1");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.RemoveLiveProduct(createlive.getId(), pid);
			queryliveproductidlistBean.setLiveId(createlive.getId());
			queryliveproductidlistCall.setData(queryliveproductidlistBean);
			queryliveproductidlistCall.callService();			
			Logger.verifyEquals("200", queryliveproductidlistCall.getString("Code"), "验证Code");
			Logger.verifyEquals(0,queryliveproductidlistCall.getProductList().size(),"验证商品数量");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}